package com.sangengblog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sangengblog.domain.ResponseResult;
import com.sangengblog.domain.entity.Link;
import com.sangengblog.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/content/link")
public class linkController {

    @Autowired
    private LinkService linkService;

    @GetMapping("/list")
    public ResponseResult list(int pageNum, int pageSize, String name, String status) {
        return linkService.getAllLink(pageNum, pageSize, name, status);
    }

    @PostMapping
    public ResponseResult add(@RequestBody Link link) {
        return linkService.addLink(link);
    }

    @GetMapping("/{id}")
    public ResponseResult getLink(@PathVariable("id") Long id) {
        return ResponseResult.okResult(linkService.getById(id));
    }

    @PutMapping
    public ResponseResult update(@RequestBody Link link) {
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Link::getId, link.getId());
        return ResponseResult.okResult(linkService.update(link,queryWrapper));
    }

    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") Long id) {
        return ResponseResult.okResult(linkService.removeById(id));
    }

}
